package com.ideal.gpt.app.docqajob.localjob;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.alibaba.fastjson.JSONObject;
import com.ideal.gpt.GptJobConfig;
import com.lzwork.gpt.utility.caller.GptCallResult;
import com.lzwork.gpt.utility.common.ParserCaller;
import com.lzwork.utility.TimeTicker;
import com.lzwork.utility.file.LineContent;
import com.lzwork.utility.logger.LOGGER;
import com.lzwork.utility.officeparser.ExcelSaver;
import com.lzwork.utility.pair.StringPair;

import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer;

/**
 * @author LiZheng
 * @date 2024年4月28日 16:36:33
 */

public class ApiTest extends ExcelSaver
{
	public ApiTest(String apiKey, boolean dolog)
	{
		String fullUrl = GptJobConfig.getProp("api_server") + "/callapi?apikey=" + apiKey;

		caller = new ParserCaller(fullUrl, dolog);
	}

	ParserCaller caller;

	public JSONObject doTest(List<StringPair> contents, int limit, int num, File output)
	{
		Workbook wb = makeXlsxFile(output, contents.size());
		Sheet sheet = wb.createSheet();
		appendLine(sheet, "Id", "File", "Response", "ErrInfo", "Cost");

		List<String> logs = new ArrayList<>();
		Consumer<String> log = e ->
		{
			LOGGER.log(LOGGER.APP, e);
			logs.add(e);
		};

		TimeTicker ticker = new TimeTicker();

		int index = 0;

		for (StringPair sp : contents)
		{
			index++;
			JSONObject param = new JSONObject();
			param.put("input", sp.second());
			param.put("num", num);

			GptCallResult cr = caller.call(param);

			if (cr.getCode() == 404)
			{
				log.accept("Remote api is 404.");
				break;
			}

			String response = cr.getResponse();
			String err = cr.getErrMessage();
			String cost = cr.getGpuCost();

			appendLine(sheet, index, sp.first(), response, err, cost);

			if (limit > 0 && index >= limit)
			{
				log.accept("Stop at count limit: " + limit);
				break;
			}

			if (index % 50 == 0)
			{
				System.out.println(index + " items checked. Cost: " + ticker.tickS());
			}
		}
		log.accept("Total " + index + " items checked.");

		saveXlsFile(wb, output);

		JSONObject result = new JSONObject(true);
		result.put("count", index);
		result.put("cost", ticker.tickAllS());
		result.put("logs", logs);

		return result;
	}

	public JSONObject doTest(File[] filePool, Map<String, String> fileIndex, int limit, int num, File outputFile)
	{
		List<StringPair> contents = new ArrayList<>();

		for (File f : filePool)
		{
			if (f.isFile())
			{
				String content = LineContent.loadString(f);
				String title = f.getName();

				if (fileIndex != null && fileIndex.containsKey(title))
					title = fileIndex.get(title);

				contents.add(new StringPair(title, content));
			}

			if (limit > 0 && contents.size() >= limit)
				break;
		}

		return doTest(contents, -1, num, outputFile);
	}

	public static void main(String[] args)
	{
		File folder = new File("V:/doc qa/doc_contents");
		File output = new File("v:/doc qa/apitest.xlsx");

		ApiTest tester = new ApiTest("pt_keywt", true);
		JSONObject obj = tester.doTest(folder.listFiles(), null, 10, 3, output);

		System.out.println(obj.toJSONString());
	}
}
